﻿
namespace UserDefinedForms.Windows.Forms
{
	/// <summary>
	/// abstract Order typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class OrderDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<OrderDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Order _currentOrder = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxOrderDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxOrderErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxOrderBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the OrderId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxOrderIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the OrderType property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxOrderTypeDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the FormId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxFormIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the CreatedDate property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxCreatedDateDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.DesignForm> _FormIdList;
		
		/// <summary> 
		/// The list of selectable DesignForm
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.DesignForm> FormIdList
		{
			get {return this._FormIdList;}
			set 
			{
				this._FormIdList = value;
				this.uxFormIdDataGridViewColumn.DataSource = null;
				this.uxFormIdDataGridViewColumn.DataSource = this._FormIdList;
			}
		}
		
		private bool _allowNewItemInFormIdList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of DesignForm
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInFormIdList
		{
			get { return _allowNewItemInFormIdList;}
			set
			{
				this._allowNewItemInFormIdList = value;
				this.uxFormIdDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Order> _OrderList;
				
		/// <summary> 
		/// The list of Order to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Order> OrderList
		{
			get {return this._OrderList;}
			set
			{
				this._OrderList = value;
				this.uxOrderBindingSource.DataSource = null;
				this.uxOrderBindingSource.DataSource = value;
				this.uxOrderDataGridView.DataSource = null;
				this.uxOrderDataGridView.DataSource = this.uxOrderBindingSource;				
				//this.uxOrderBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxOrderBindingSource_ListChanged);
				this.uxOrderBindingSource.CurrentItemChanged += new System.EventHandler(OnOrderBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnOrderBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentOrder = uxOrderBindingSource.Current as Entities.Order;
			
			if (_currentOrder != null)
			{
				_currentOrder.Validate();
			}
			//_Order.Validate();
			OnCurrentEntityChanged();
		}

		//void uxOrderBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <see cref="Entities.Order"/> instance.
		/// </summary>
		public Entities.Order SelectedOrder
		{
			get {return this._currentOrder;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxOrderDataGridView.VirtualMode;}
			set
			{
				this.uxOrderDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxOrderDataGridView.AllowUserToAddRows;}
			set {this.uxOrderDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxOrderDataGridView.AllowUserToDeleteRows;}
			set {this.uxOrderDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <see cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxOrderDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxOrderDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="OrderDataGridViewBase"/> class.
		/// </summary>
		public OrderDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxOrderDataGridView = new System.Windows.Forms.DataGridView();
			this.uxOrderBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxOrderErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxOrderIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxOrderTypeDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxFormIdDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxCreatedDateDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxFormIdBindingSource = new DesignFormBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxFormIdBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxOrderDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxOrderBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxOrderErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxOrderErrorProvider
			// 
			this.uxOrderErrorProvider.ContainerControl = this;
			this.uxOrderErrorProvider.DataSource = this.uxOrderBindingSource;						
			// 
			// uxOrderDataGridView
			// 
			this.uxOrderDataGridView.AutoGenerateColumns = false;
			this.uxOrderDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxOrderDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxOrderIdDataGridViewColumn,
		this.uxOrderTypeDataGridViewColumn,
		this.uxFormIdDataGridViewColumn,
		this.uxCreatedDateDataGridViewColumn			});
			this.uxOrderDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxOrderDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxOrderDataGridView.Name = "uxOrderDataGridView";
			this.uxOrderDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxOrderDataGridView.TabIndex = 0;	
			this.uxOrderDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxOrderDataGridView.EnableHeadersVisualStyles = false;
			this.uxOrderDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnOrderDataGridViewDataError);
			this.uxOrderDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnOrderDataGridViewCellValueNeeded);
			this.uxOrderDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnOrderDataGridViewCellValuePushed);
			
			//
			// uxOrderIdDataGridViewColumn
			//
			this.uxOrderIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxOrderIdDataGridViewColumn.DataPropertyName = "OrderId";
			this.uxOrderIdDataGridViewColumn.HeaderText = "OrderId";
			this.uxOrderIdDataGridViewColumn.Name = "uxOrderIdDataGridViewColumn";
			this.uxOrderIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxOrderIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxOrderIdDataGridViewColumn.ReadOnly = true;		
			//
			// uxOrderTypeDataGridViewColumn
			//
			this.uxOrderTypeDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxOrderTypeDataGridViewColumn.DataPropertyName = "OrderType";
			this.uxOrderTypeDataGridViewColumn.HeaderText = "OrderType";
			this.uxOrderTypeDataGridViewColumn.Name = "uxOrderTypeDataGridViewColumn";
			this.uxOrderTypeDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxOrderTypeDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxOrderTypeDataGridViewColumn.ReadOnly = false;		
			//
			// uxFormIdDataGridViewColumn
			//
			this.uxFormIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxFormIdDataGridViewColumn.DataPropertyName = "FormId";
			this.uxFormIdDataGridViewColumn.HeaderText = "FormId";
			this.uxFormIdDataGridViewColumn.Name = "uxFormIdDataGridViewColumn";
			this.uxFormIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxFormIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxFormIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxCreatedDateDataGridViewColumn
			//
			this.uxCreatedDateDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxCreatedDateDataGridViewColumn.DataPropertyName = "CreatedDate";
			this.uxCreatedDateDataGridViewColumn.HeaderText = "CreatedDate";
			this.uxCreatedDateDataGridViewColumn.Name = "uxCreatedDateDataGridViewColumn";
			this.uxCreatedDateDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxCreatedDateDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxCreatedDateDataGridViewColumn.ReadOnly = false;		
			//
			// uxFormIdDataGridViewColumn
			//				
			this.uxFormIdDataGridViewColumn.DisplayMember = "Name";	
			this.uxFormIdDataGridViewColumn.ValueMember = "FormId";	
			this.uxFormIdDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxFormIdDataGridViewColumn.DataSource = uxFormIdBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxOrderDataGridView);
			this.Name = "OrderDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxFormIdBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxOrderErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxOrderDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxOrderBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				OrderDataGridViewEventArgs args = new OrderDataGridViewEventArgs();
				args.Order = _currentOrder;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class OrderDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Order	_Order;
	
			/// <summary>
			/// the  Entities.Order instance.
			/// </summary>
			public Entities.Order Order
			{
				get { return _Order; }
				set { _Order = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxOrderDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnOrderDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxOrderDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnOrderDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxOrderDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxOrderIdDataGridViewColumn":
						e.Value = OrderList[e.RowIndex].OrderId;
						break;
					case "uxOrderTypeDataGridViewColumn":
						e.Value = OrderList[e.RowIndex].OrderType;
						break;
					case "uxFormIdDataGridViewColumn":
						e.Value = OrderList[e.RowIndex].FormId;
						break;
					case "uxCreatedDateDataGridViewColumn":
						e.Value = OrderList[e.RowIndex].CreatedDate;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxOrderDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnOrderDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxOrderDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxOrderIdDataGridViewColumn":
						OrderList[e.RowIndex].OrderId = (System.Int32)e.Value;
						break;
					case "uxOrderTypeDataGridViewColumn":
						OrderList[e.RowIndex].OrderType = (System.Int32)e.Value;
						break;
					case "uxFormIdDataGridViewColumn":
						OrderList[e.RowIndex].FormId = (System.Int32)e.Value;
						break;
					case "uxCreatedDateDataGridViewColumn":
						OrderList[e.RowIndex].CreatedDate = (System.DateTime?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
